home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / demos / spin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-26  |  2.8 KB  |  164 lines

  1. /* spin.c */
  2.  
  3.  
  4. /*
  5.  * Spinning box.  This program is in the public domain.
  6.  *
  7.  * Brian Paul
  8.  */
  9.  
  10.  
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include "gltk.h"
  14.  
  15.  
  16.  
  17.  
  18. static GLfloat Xrot, Xstep;
  19. static GLfloat Yrot, Ystep;
  20. static GLfloat Zrot, Zstep;
  21. static GLfloat Step = 5.0;
  22. static GLfloat Scale = 1.0;
  23. static GLuint Object;
  24.  
  25.  
  26.  
  27.  
  28. static GLuint make_object( void )
  29. {
  30.    GLuint list;
  31.  
  32.    list = glGenLists( 1 );
  33.  
  34.    glNewList( list, GL_COMPILE );
  35.  
  36.    glBegin( GL_LINE_LOOP );
  37.    glVertex3f(  1.0,  0.5, -0.4 );
  38.    glVertex3f(  1.0, -0.5, -0.4 );
  39.    glVertex3f( -1.0, -0.5, -0.4 );
  40.    glVertex3f( -1.0,  0.5, -0.4 );
  41.    glEnd();
  42.  
  43.    glBegin( GL_LINE_LOOP );
  44.    glVertex3f(  1.0,  0.5, 0.4 );
  45.    glVertex3f(  1.0, -0.5, 0.4 );
  46.    glVertex3f( -1.0, -0.5, 0.4 );
  47.    glVertex3f( -1.0,  0.5, 0.4 );
  48.    glEnd();
  49.  
  50.    glBegin( GL_LINES );
  51.    glVertex3f(  1.0,  0.5, -0.4 );   glVertex3f(  1.0,  0.5, 0.4 );
  52.    glVertex3f(  1.0, -0.5, -0.4 );   glVertex3f(  1.0, -0.5, 0.4 );
  53.    glVertex3f( -1.0, -0.5, -0.4 );   glVertex3f( -1.0, -0.5, 0.4 );
  54.    glVertex3f( -1.0,  0.5, -0.4 );   glVertex3f( -1.0,  0.5, 0.4 );
  55.    glEnd();
  56.  
  57.  
  58.    glEndList();
  59.  
  60.    return list;
  61. }
  62.  
  63.  
  64.  
  65. static void reshape( int width, int height )
  66. {
  67.    glViewport(0, 0, (GLint)width, (GLint)height);
  68.    glMatrixMode(GL_PROJECTION);
  69.    glLoadIdentity();
  70.    glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
  71.    glMatrixMode(GL_MODELVIEW);
  72. }
  73.  
  74.  
  75. static GLenum key(int k, GLenum mask)
  76. {
  77.    switch (k) {
  78.       case TK_ESCAPE:
  79.      tkQuit();
  80.    }
  81.    return GL_FALSE;
  82. }
  83.  
  84.  
  85. static void idle( void )
  86. {
  87.    Xrot += Xstep;
  88.    Yrot += Ystep;
  89.    Zrot += Zstep;
  90.  
  91.    if (Xrot>=360.0) {
  92.       Xrot = Xstep = 0.0;
  93.       Ystep = Step;
  94.    }
  95.    else if (Yrot>=360.0) {
  96.       Yrot = Ystep = 0.0;
  97.       Zstep = Step;
  98.    }
  99.    else if (Zrot>=360.0) {
  100.       Zrot = Zstep = 0.0;
  101.       Xstep = Step;
  102.    }
  103.  
  104. }
  105.  
  106.  
  107.  
  108. static void draw( void )
  109. {
  110.    glClear( GL_COLOR_BUFFER_BIT );
  111.  
  112.    glPushMatrix();
  113.  
  114.    glTranslatef( 0.0, 0.0, -10.0 );
  115.    glScalef( Scale, Scale, Scale );
  116.    if (Xstep) {
  117.       glRotatef( Xrot, 1.0, 0.0, 0.0 );
  118.    }
  119.    else if (Ystep) {
  120.       glRotatef( Yrot, 0.0, 1.0, 0.0 );
  121.    }
  122.    else {
  123.       glRotatef( Zrot, 0.0, 0.0, 1.0 );
  124.    }
  125.  
  126.    glCallList( Object );
  127.  
  128.    glPopMatrix();
  129.  
  130.    glFlush();
  131.    tkSwapBuffers();
  132. }
  133.  
  134.  
  135. main( int argc, char *argv[] )
  136. {
  137.    tkInitPosition(0, 0, 300, 300);
  138.  
  139.    tkInitDisplayMode( TK_DOUBLE | TK_DIRECT | TK_RGB );
  140.  
  141.    if (tkInitWindow("Spin") == GL_FALSE) {
  142.       tkQuit();
  143.    }
  144.  
  145.    Object = make_object();
  146.    glCullFace( GL_BACK );
  147. /*   glEnable( GL_CULL_FACE );*/
  148.    glDisable( GL_DITHER );
  149.    glShadeModel( GL_FLAT );
  150.  
  151.    glColor3f( 1.0, 1.0, 1.0 );
  152.  
  153.    Xrot = Yrot = Zrot = 0.0;
  154.    Xstep = Step;
  155.    Ystep = Zstep = 0.0;
  156.  
  157.    tkExposeFunc( reshape );
  158.    tkReshapeFunc( reshape );
  159.    tkKeyDownFunc( key );
  160.    tkIdleFunc( idle );
  161.    tkDisplayFunc( draw );
  162.    tkExec();
  163. }
  164.